<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;

class AdminPhoto extends Model
{
   // 获取所有路径
   public static function getPath()
   {
		$path = public_path().'upload'.DS;
		if(!is_dir($path)){
			@mkdir($path,0777,true);
		}

		$data=[];
		foreach (scandir($path) as $k) {
			if(is_dir($path.$k) && $k!="." &&$k!=".."){
				$data[] = ['name'=>$k];
			}
		}
		
		// //如果没有默认路径就创建
		// if(empty($data)){
		// 	$path .= 'images'.DS;
		// 	@mkdir($path);
		// 	$data=[
		// 		['name'=>'images'],
		// 	];
		// }

		return ['code'=>0,'data'=>$data];
   }

    // 获取列表
    public static function getAll($where=[])
    {
        $limit = I('get.limit/d');
        if ($search = I('get.path')) {
            $where[] =  ['path', '=',$search];
        }
        $list = self::order('id','desc')->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    // 获取列表
    public static function getList($where)
    {
        $limit = I('get.limit/d');
        $list = self::order('id','desc')->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    public function getTypeAttr($value)
    {
        $type = ['1' => '本地', '2' => '阿里云','3'=>'七牛云'];
        return $type[$value];
    }
	
	public function getGetTypeAttr($value)
	{
	    $type = ['1' => '上传', '2' => '抓图'];
	    return $type[$value];
	}
	
	/** 获取器 **/
	
	public function getSizeAttr($value){
		$value=format_size($value,'mb')." MB";
	    return $value;
	}
}